package com.basker.pisces.trace;

import java.util.Objects;

import com.basker.pisces.trace.annotation.EnableTrace.TraceEvent;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString(of = {"traceId", "id", "parentId", "name", "tag"})
public class Span {

    private String id;
    private String parentId;
    private String traceId;
    private String name;
    private Tag tag;
    private TraceEvent[] traceEvents = new TraceEvent[0];
    private Long start;
    private Long end;

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (this == obj) {
            return true;
        }

        if (!obj.getClass().equals(this.getClass())) {
            return false;
        }

        return Objects.equals(this.id, ((Span) obj).id);
    }

    public Long getDuration() {
        if (start == null) {
            return null;
        }

        if (end == null) {
            return System.currentTimeMillis() - start;
        }

        return end - start;
    }

    @Override
    public int hashCode() {
        if (this.id != null) {
            return id.hashCode();
        }

        return super.hashCode();
    }

    public static enum Tag {
        Clinet, Server, Noop
    }
}
